/***************************************************************************
 *   Copyright (C) 2007 by Ziman & Xloverin                                *
 *   ziman@centrum.sk, xloverin@gmail.com                                  *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/


#include "newquery.h"
#include <QMessageBox>
#include <QTextStream>

void constructAccept(Query::Prefs &result)
{
	QTextStream re(&result.acceptRE, QIODevice::WriteOnly);
	re << 	"^(" <<

			// tell
			result.incoming << " ti povedal[ao]?: " << "|" <<
			"Povedal[ao]? si " << result.outgoing << ": " << "|" <<

			// pemote
			"\\(" << result.outgoing << "\\) >> " << "|" <<
			">> " << result.incoming << " " << "|" <<

			// kiss
			"\\*CMUK!\\* Pobozkal[ao]? si " << result.subject << "|" <<
			"\\*CMUK!\\* " << result.incoming << " sa s Tebou bozkava! \\*CMUK!\\*" << "|" <<
			"\\*CMUK!\\* " << result.incoming << " ta bozkava: " << "|" <<
			"Pobozkal[ao]? si " << result.subject << "|" <<
			result.incoming << " ta pobozkal" << "|" <<

			// hug
			"Objal[ao]? si " << result.subject << "|" <<
			".*\\*--\\* H U G \\*--\\* Pozdravuje Ta " << result.incoming << "! \\*--\\* H U G \\*--\\*" << "|" <<
			result.incoming << " ta objal" << "|" <<

			// login/logout
			result.incoming << " sa Ti prave (pri|od)hlasil[ao]?"
			")";
}

void NewQuery::on_cbNick_currentIndexChanged(const QString &nickName)
{
	const NickList::Nick *nick = nickList[nickName];
	if (nick == NULL) return;
	
	leSubject->setText(nick->a);
	leIncoming->setText(nick->nick);
	leOutgoing->setText(nick->d);
	leTitle->setText("Query: " + nick->nick);
	lePrefix->setText(".tell " + nick->nick + " ");
}

NewQuery::NewQuery(QWidget *parent, const NickList &nickList_)
		: QDialog(parent), Ui::newQuery(), nickList(nickList_)
{
	setupUi(this);
	
	cbNick->addItems(nickList.list());
	
	cbNick->setCurrentIndex(-1);
	cbNick->setEditText("");
	leOutgoing->setText("");
	lePrefix->setText(".tell ");
	leSubject->setText("");
	leTitle->setText("Query");
	leIncoming->setText("");
}

NewQuery::~NewQuery()
{
}

void NewQuery::on_cbNick_editTextChanged(QString text)
{
	leTitle->setText(tr("Query: ") + text);
	lePrefix->setText(".tell " + text + " ");
}

void NewQuery::setQueryPrefs(const Query::Prefs &prefs)
{
	cbNick->setEditText(prefs.nick);
	leIncoming->setText(prefs.incoming);
	leOutgoing->setText(prefs.outgoing);
	leTitle->setText(prefs.title);
	lePrefix->setText(prefs.prefix);
	leSubject->setText(prefs.subject);
	cbFilterMatching->setCheckState(prefs.filterMatching ? Qt::Checked : Qt::Unchecked);
}

Query::Prefs NewQuery::queryPrefs()
{
	Query::Prefs result;

	result.nick = cbNick->currentText();
	result.incoming = leIncoming->text();
	result.outgoing = leOutgoing->text();
	result.filterMatching = (cbFilterMatching->checkState() == Qt::Checked);
	result.title = leTitle->text();
	result.prefix = lePrefix->text();
	result.subject = leSubject->text();

	constructAccept(result);

	return result;
}

void NewQuery::reject()
{
	QDialog::reject();
}

void NewQuery::accept()
{
	if (
		cbNick->currentText().isEmpty() || leIncoming->text().isEmpty() ||
		   leOutgoing->text().isEmpty()
	   )
	{
		QMessageBox::critical(this, tr("Invalid values"), tr("Some fields are empty!"));
	} else {
		QDialog::accept();
	}
}



